package com.capgemini.teamtwo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.capgemini.teamtwo.entity.ProjectInfo;
import com.capgemini.teamtwo.entity.ProjectMember;
import com.capgemini.teamtwo.entity.User;
import com.capgemini.teamtwo.mapper.ProjectInfoMapper;
import com.capgemini.teamtwo.service.ProjectInfoService;
import com.capgemini.teamtwo.service.ProjectMemberService;
import com.capgemini.teamtwo.service.ProjectScheduleService;
import com.capgemini.teamtwo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ProjectInfoServiceImpl extends ServiceImpl<ProjectInfoMapper, ProjectInfo> implements ProjectInfoService {


    @Autowired
    private ProjectMemberService projectMemberService;
    @Autowired
    private UserService userService;

    @Transactional
    public boolean saveAfterCalculate(ProjectInfo projectInfo) {

        boolean reasult=this.save(projectInfo);
        log.info("projectInfo的主键id："+projectInfo.getId());
        //这里添加空值异常处理

//        //查询项目人数并保存
//        LambdaQueryWrapper<ProjectMember> projectMemberLambdaQueryWrapper=new LambdaQueryWrapper<>();
//        projectMemberLambdaQueryWrapper.eq(ProjectMember::getProjectId,projectInfo.getId());
//        projectInfo.setMemberNumber(projectMemberService.count(projectMemberLambdaQueryWrapper));

//        //计算项目进度并保存
//        LambdaQueryWrapper<ProjectSchedule> projectScheduleLambdaQueryWrapper=new LambdaQueryWrapper<>();
//        projectScheduleLambdaQueryWrapper.eq(ProjectSchedule::getProjectId,projectInfo.getId());
//        List<ProjectSchedule> actualWork=projectScheduleService.list(projectScheduleLambdaQueryWrapper);
//        Integer actualTotal=actualWork.stream().mapToInt(ProjectSchedule::getActual).sum();
//        Integer planTotal=actualWork.stream().mapToInt(ProjectSchedule::getPlan).sum();
//        if(planTotal!=0) projectInfo.setRate((actualTotal/planTotal)*100);
//        else
//            projectInfo.setRate(0);
//        //end
        //将user表中改leader的projectid更新
        User leader=userService.getById(projectInfo.getLeaderId());
        leader.setProjectId(projectInfo.getId());
        leader.setStatus("On Project");
        userService.updateById(leader);
        //
//        ProjectMember projectMember=new ProjectMember();
//        projectMember.setProjectId(projectInfo.getId());
//        projectMember.setMemberId(projectInfo.getLeaderId());
//        projectMemberService.save(projectMember);

        //boolean reasult=this.updateById(projectInfo);
        //log.info("实际完成工作："+actualTotal);
        return reasult;
    }


    @Transactional
    public boolean updateWithOtherTable(ProjectInfo projectInfo) {
        boolean result=this.updateById(projectInfo);
        //将user表中改leader的projectid更新
        User leader=userService.getById(projectInfo.getLeaderId());
        leader.setProjectId(projectInfo.getId());
        leader.setStatus("On Project");
        userService.updateById(leader);
        //将项目——员工表更新
        LambdaQueryWrapper<ProjectMember> projectMemberLambdaQueryWrapper=new LambdaQueryWrapper<>();
        projectMemberLambdaQueryWrapper.eq(ProjectMember::getMemberId,projectInfo.getLeaderId());
        projectMemberLambdaQueryWrapper.eq(ProjectMember::getProjectId,projectInfo.getId());
        ProjectMember projectMember=projectMemberService.getOne(projectMemberLambdaQueryWrapper);
        if(projectMember==null) {
            projectMember=new ProjectMember();
            projectMember.setProjectId(projectInfo.getId());
            projectMember.setMemberId(projectInfo.getLeaderId());
            projectMemberService.save(projectMember);
        }
        return result;
    }


    @Transactional
    public boolean removeWithOtherTable(Integer projectId) {
        boolean result=this.removeById(projectId);
        //删除项目需要释放员工
        LambdaQueryWrapper<ProjectMember> projectMemberLambdaQueryWrapper=new LambdaQueryWrapper<>();
        projectMemberLambdaQueryWrapper.eq(ProjectMember::getProjectId,projectId);
        projectMemberService.remove(projectMemberLambdaQueryWrapper);
        //在user表中更新员工状态
        LambdaQueryWrapper<User> userLambdaQueryWrapper=new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getProjectId,projectId);
        List<User> members=userService.list(userLambdaQueryWrapper);
        members=members.stream().map( item ->{
            item.setProjectId(0);
            item.setStatus("Bench");
            return item;
        }).collect(Collectors.toList());
        userService.updateBatchById(members);
        return result;
    }
}